<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>添加权限</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<link rel="stylesheet" href="../../libme/modules/dtree/dtree.css">
	<link rel="stylesheet" href="../../libme/modules/dtree/font/dtreefont.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body>
<div class="ok-body">
	<!--form表单-->
	<!--<form class="layui-form layui-form-pane ok-form">-->
	<form class="layui-form ok-form" lay-filter="permission-add">
		<div class="layui-form-item">
			<label class="layui-form-label">权限名称 </label>
			<div class="layui-input-block">
				<input type="text" name="permissionName" placeholder="权限中文名称" autocomplete="off" class="layui-input" lay-verify="required" lay-verType="tips" id="permissionName">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">权限Code </label>
			<div class="layui-input-block">
				<input type="text" name="permissionCode" placeholder="权限英文代号，如 bus:model:create" autocomplete="off" class="layui-input" lay-verify="required|permissionCode" lay-verType="tips" id="permissionCode">
			</div>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">上级权限 </label>
			<div class="layui-input-inline">
				<ul id="permissions" class="dtree" data-id="0" ></ul>
			</div>
			<button type="button" class="layui-btn layui-btn-primary" id="preset">顶级权限</button>
		</div>

		<div class="layui-form-item">
			<label class="layui-form-label">备注 </label>
			<div class="layui-input-block">
				<input type="text" name="remark" placeholder="用户什么也没有留下" autocomplete="off" class="layui-input">
			</div>
		</div>

		<div class="layui-form-item">
			<div class="layui-input-block">
				<button class="layui-btn" lay-submit lay-filter="add">立即提交</button>
				<button type="reset" class="layui-btn layui-btn-primary">重置</button>
			</div>
		</div>
	</form>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script src="../../libme/pages/body.js"></script>
<script src="../../libme/script/pinyin.js"></script>
<script type="text/javascript">
	layui.use(["element", "form", "jquery", "okMock", "api", "dtree"], function () {
		let form = layui.form;
        let dtree = layui.dtree;
        let okMock = layui.okMock;
        let $ = layui.jquery;
        let api = layui.api;

        okLoading.close();

		// tree.render({
        // 	elem: "#permissionTree",
        // 	//data: okMock.api.permission.list,
        // 	data: data,//从本地 localStorage 中取值
        // 	showCheckbox: true
        // });

		form.verify({
            permissionCode: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z0-9_:]+$").test(value)){
                    return '权限标识代号不能有特殊字符';
                }
                if(/^\d+$/.test(value)){
                    return '权限标识代号不能使用数字';
                }
                if(!/^([A-Za-z]+):([A-Za-z]+):([A-Za-z]+)$/.test(value)){
                    return '权限标识代号不符合要求';
				}
            }
		})
        form.on("submit(add)", function (data) {
			// TODO 权限节点校验
			api.request({
				url:okMock.api.permission.create,
				where: data.field,
				dataType: api.jsonType.ujson
			},function (resp, stat) {
                api.greenTickMsg("添加成功", function () {
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                });
            })
			return false;
		});

        var permDtree = dtree.render({
            elem: "#permissions",
            url: okMock.api.permission.dtree,
            dataStyle: "layuiStyle",
            dataFormat: "list",
            response:{
                statusCode: 100,
            },
            selectTips: "请选择权限",
            initLevel : 1,
            line: true,
            width: "100%",
            select: true,
            selectCardHeight: "150",
            selectInputName: {
                nodeId: "parentId",
            },
            done: function(res, $ul, first){
                if(first) {}
            }
        });
        dtree.on('node("permissions")', function(obj){
            // param 中的成员 : leaf/parentId/noteId/context/level/spread
            var param = dtree.selectVal("permissions");
        });

        $("#preset,#reset").click(function(){
            pReset();
        });
        function pReset() {
            permDtree.selectResetVal();
        }
        $("body").on("click", function(event){
            $("div[dtree-id][dtree-select]").removeClass("layui-form-selected");
            $("div[dtree-id][dtree-card]").removeClass("dtree-select-show layui-anim layui-anim-upbit");
        });



        $(function () {//页面加载
        })
	})
</script>
</body>
</html>
